package com.lyz.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lyz.common.QueryPageParam;
import com.lyz.common.Result;
import com.lyz.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import com.lyz.entity.User;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    //登录界面
    @PostMapping("/login")
    public Result login(@RequestBody User user) {
        // 查询用户信息
        User dbUser = userService.lambdaQuery()
                .eq(User::getNo, user.getNo())
                .eq(User::getPassword, user.getPassword())
                .one();

        // 验证用户是否存在
        if (dbUser == null) {
            return Result.fail("用户名或密码错误");
        }

        // 登录成功，返回用户信息或token
        return Result.success(dbUser);
    }
    //用户列表
    @GetMapping("/list")
    public List<User> list() {
        return userService.list();
    }

    // 新增
    @PostMapping("/save")
    public Result save(@RequestBody User user) {
        return userService.save(user) ? Result.success() : Result.fail();
    }

    //修改
    @PostMapping("/update")
    public Result update(@RequestBody User user) {
        return userService.updateById(user) ? Result.success() : Result.fail();
    }

    //删除
    @PostMapping("/delete")
    public Result delete(@RequestBody Map<String, Integer> params) {
        Integer id = params.get("id");
        return userService.removeById(id) ? Result.success() : Result.fail();
    }

    //模糊查询
    @PostMapping("/query")
    public Result query(@RequestBody User user) {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper();

        // 如果用户名不为空，则添加模糊查询条件
        if (!StringUtils.isEmpty(user.getName())) {
            lambdaQueryWrapper.like(User::getName, user.getName());
        }
        // 如果用户名为空，不添加条件，将返回所有用户
        List<User> userList = userService.list(lambdaQueryWrapper);
        return Result.success(userList);
    }


    //分页查询
    @PostMapping("/listPage")
    public Result listPage(@RequestBody QueryPageParam query) {
        HashMap param = query.getParam();
        String name = param.get("name") == null ? "" : param.get("name").toString();
        String sex = param.get("sex") == null ? "" : param.get("sex").toString();

        Page<User> page = new Page();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.like(User::getName, name);
        lambdaQueryWrapper.like(User::getSex, sex);
        IPage<User> result = userService.page(page, lambdaQueryWrapper);
        System.out.println("total==" + result.getTotal());
        return Result.sucess(result.getRecords(), result.getTotal());

    }
}

